package cn.iocoder.yudao.module.common.controller.admin.gradesubjectrelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.gradesubjectrelation.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.gradesubjectrelation.GradeSubjectRelationDO;
import cn.iocoder.yudao.module.common.service.gradesubjectrelation.GradeSubjectRelationService;

@Tag(name = "管理后台 - 年级学科")
@RestController
@RequestMapping("/common/grade-subject-relation")
@Validated
public class GradeSubjectRelationController {

    @Resource
    private GradeSubjectRelationService gradeSubjectRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建年级学科")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:create')")
    public CommonResult<Long> createGradeSubjectRelation(@Valid @RequestBody GradeSubjectRelationSaveReqVO createReqVO) {
        return success(gradeSubjectRelationService.createGradeSubjectRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新年级学科")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:update')")
    public CommonResult<Boolean> updateGradeSubjectRelation(@Valid @RequestBody GradeSubjectRelationSaveReqVO updateReqVO) {
        gradeSubjectRelationService.updateGradeSubjectRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年级学科")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:delete')")
    public CommonResult<Boolean> deleteGradeSubjectRelation(@RequestParam("id") Long id) {
        gradeSubjectRelationService.deleteGradeSubjectRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年级学科")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:query')")
    public CommonResult<GradeSubjectRelationRespVO> getGradeSubjectRelation(@RequestParam("id") Long id) {
        GradeSubjectRelationDO gradeSubjectRelation = gradeSubjectRelationService.getGradeSubjectRelation(id);
        return success(BeanUtils.toBean(gradeSubjectRelation, GradeSubjectRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年级学科分页")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:query')")
    public CommonResult<PageResult<GradeSubjectRelationRespVO>> getGradeSubjectRelationPage(@Valid GradeSubjectRelationPageReqVO pageReqVO) {
        PageResult<GradeSubjectRelationDO> pageResult = gradeSubjectRelationService.getGradeSubjectRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GradeSubjectRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出年级学科 Excel")
    @PreAuthorize("@ss.hasPermission('common:grade-subject-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportGradeSubjectRelationExcel(@Valid GradeSubjectRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GradeSubjectRelationDO> list = gradeSubjectRelationService.getGradeSubjectRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "年级学科.xls", "数据", GradeSubjectRelationRespVO.class,
                        BeanUtils.toBean(list, GradeSubjectRelationRespVO.class));
    }

}